package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoClass {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;
    public StuDaoClass()
    {
        dbc = new DBConnection();

    }
    // 全查询班级信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<StuEntity>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from classinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity class01 = new StuEntity();
                class01.setCid(rs.getInt("cid"));
                class01.setGid(rs.getInt("gid"));
                class01.setXid(rs.getInt("xid"));
                class01.setCname(rs.getString("cname"));
                ar.add(class01);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }

    // 查询一个班级信息表对象
    public StuEntity getOneById(int cid)
    {
        StuEntity class01 = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from classinfo where cid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, cid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                class01.setCid(rs.getInt("cid"));
                class01.setGid(rs.getInt("gid"));
                class01.setXid(rs.getInt("xid"));
                class01.setCname(rs.getString("cname"));

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return class01;
    }

    // 增加班级信息表对象
    public void addStu(StuEntity class01)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into classinfo values(?,?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, class01.getCid());
            pre.setInt(2,class01.getGid());
            pre.setInt(3,class01.getXid());
            pre.setString(4, class01.getCname());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 修改年级信息表对象
    public void updateStu(StuEntity stu1)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  classinfo  set gid=?,xid=?, cname=? where cid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, stu1.getGid());
            pre.setInt(2, stu1.getXid());
            pre.setString(3, stu1.getCname());
            pre.setInt(4, stu1.getCid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 删除班级信息表对象
    public void delStu(int Cid)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "delete from classinfo where cid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,Cid);
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}

